package com.ocom.accesservice.controller;

import com.github.pagehelper.PageInfo;
import com.ocom.accesservice.service.YkmCenterConfigService;
import com.ocom.accesservice.vo.ykmcenterconfig.GetCenterConfigVo;
import com.ocom.common.constant.AccessServiceUrl;
import com.ocom.common.entity.master.EntityYkmCenterConfig;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class YkmCenterConfigController {

    Logger log= LoggerFactory.getLogger(YkmCenterConfigController.class);

    @Autowired
    YkmCenterConfigService ykmCenterConfigService;

    /**
     * 获取中台配置信息（getCenterConfig）【WEB】
     * */
    @PostMapping(AccessServiceUrl.URL_GetCenterConfig)
    public Result GetCenterConfig(@Validated @RequestBody GetCenterConfigVo getCenterConfigVo){
        if (getCenterConfigVo.getComId() == null){
            log.error("单位编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"单位编号为空");
        }
        if (getCenterConfigVo.getPage() == null){
            log.error("页码page为空");
            return ResultUtil.error(ResultEnum.Err_203,"页码page为空");
        }
        if (getCenterConfigVo.getSize() == null){
            log.error("页数size为空");
            return ResultUtil.error(ResultEnum.Err_203,"页数size为空");
        }
        Map<String, Object> map = new HashMap<>();
        try {
            List<EntityYkmCenterConfig> entityYkmCenterConfigs = ykmCenterConfigService.GetCenterConfig(getCenterConfigVo.getComId(), getCenterConfigVo);
            PageInfo<EntityYkmCenterConfig> entityYkmCenterConfigPageInfo = new PageInfo<>(entityYkmCenterConfigs);

            map.put("total",entityYkmCenterConfigPageInfo.getTotal());
            map.put("list",entityYkmCenterConfigs);
        }catch (Exception exception){
            log.error("获取中台配置信息失败："+exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"获取中台配置信息失败");
        }
        return ResultUtil.success(map);
    }

    /**
     * 修改中台配置信息（updateCenterConfig）【WEB】
     * */
    @PostMapping(AccessServiceUrl.URL_UpdateCenterConfig)
    public Result UpdateCenterConfig(@Validated @RequestBody EntityYkmCenterConfig entityYkmCenterConfig){
        if (entityYkmCenterConfig.getComId() == null){
            log.error("单位编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"单位编号为空");
        }
        if (entityYkmCenterConfig.getRegionId() == null){
            log.error("片区id为空");
            return ResultUtil.error(ResultEnum.Err_203,"片区id为空");
        }
        if (entityYkmCenterConfig.getAreaId() == null){
            log.error("区域id为空");
            return ResultUtil.error(ResultEnum.Err_203,"区域id为空");
        }
        if (entityYkmCenterConfig.getHourTime() == null){
            log.error("配置信息修改,定时查询时间为空");
            return ResultUtil.error(ResultEnum.Err_203,"配置信息修改,定时查询时间为空");
        }
        if (entityYkmCenterConfig.getId() == null){
            log.error("配置信息ID为空");
            return ResultUtil.error(ResultEnum.Err_203,"配置信息ID为空");
        }
        if (entityYkmCenterConfig.getStatus() == null){
            log.error("配置信息状态为空");
            return ResultUtil.error(ResultEnum.Err_203,"配置信息状态为空");
        }
        try {
            ykmCenterConfigService.UpdateCenterConfig(entityYkmCenterConfig.getComId(),entityYkmCenterConfig);
        }catch (Exception exception){
            log.error("修改中台配置信息失败："+exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"修改中台配置信息失败");
        }
        return ResultUtil.success();
    }

    /**
     * 新增中台配置信息（addCenterConfig）【WEB】
     * */
    @PostMapping(AccessServiceUrl.URL_AddCenterConfig)
    public Result AddCenterConfig(@Validated @RequestBody EntityYkmCenterConfig entityYkmCenterConfig){
        if (entityYkmCenterConfig.getComId() == null){
            log.error("单位编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"单位编号为空");
        }
        if (entityYkmCenterConfig.getRegionId() == null){
            log.error("片区id为空");
            return ResultUtil.error(ResultEnum.Err_203,"片区id为空");
        }
        if (entityYkmCenterConfig.getAreaId() == null){
            log.error("区域id为空");
            return ResultUtil.error(ResultEnum.Err_203,"区域id为空");
        }
        if (entityYkmCenterConfig.getHourTime() == null){
            log.error("配置信息修改,定时查询时间为空");
            return ResultUtil.error(ResultEnum.Err_203,"配置信息修改,定时查询时间为空");
        }
        try {
            ykmCenterConfigService.AddCenterConfig(entityYkmCenterConfig.getComId(),entityYkmCenterConfig);
        }catch (Exception exception){
            log.error("新增中台配置信息失败："+exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"新增中台配置信息失败");
        }
        return ResultUtil.success();
    }

    /**
     * 删除中台配置信息（deleteCenterConfig）【WEB】
     * */
    @PostMapping(AccessServiceUrl.URL_DeleteCenterConfig)
    public Result DeleteCenterConfig(Long comId, String ids){
        if (comId == null){
            log.error("单位编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"单位编号为空");
        }
        if (ids.isEmpty()){
            log.error("配置信息删除ids为空");
            return ResultUtil.error(ResultEnum.Err_203,"配置信息删除ids为空");
        }
        List<Long> idList = new ArrayList<>();
        String[] idsArr = ids.split(",");
        for (String id: idsArr) {
            idList.add(Long.valueOf(id));
        }
        try {
            ykmCenterConfigService.DeleteCenterConfig(comId,idList);
        }catch (Exception exception){
            log.error("删除中台配置信息失败："+exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"删除中台配置信息失败");
        }
        return ResultUtil.success();
    }
}
